\pard\tx960\tx1920\tx2880\tx3840\tx4800\tx5760\tx6720\tx7680\tx8640\tx9600\f0\b0\i0\ulnone\fs28\fc1\cf1 Q: The gdb debugger refuses to display the value of my variable “x.” Following is the source code and the gdb output:\
\pard\tx960\tx1920\tx2880\tx3840\tx4800\tx5760\tx6720\tx7680\tx8640\tx9600\fc1\cf1 A: When code is compiled with the -O flag, the compiler can take liberties to optimize away certain source code variables. This is accomplished by careful juggling of registers and order of expression evaluation. For example, stack variables which never have their address taken and are used only across a very few instructions can “disappear” without a trace. Try compiling your code without optimization and see if this eliminates some of these surprises. The standard “make <appname>” rules do invoke optimization as shown by the -O flag:\
\pard\tx960\tx1920\tx2880\tx3840\tx4800\tx5760\tx6720\tx7680\tx8640\tx9600\fc1\cf1 The “Debug” button in ProjectBuilder does not assume “make debug.” You have to add “debug” to the Args text field to pass it as an argument to the make process.\
\
The man page for
\b cc
\b0 contains more detail over how one can control compiler optimizations; a small section is repeated here:\
\pard\tx1540\tx1800\tx2240\tx2800\tx3360\tx3920\tx4480\tx4580\tx5040\tx5600\f1\fs24\fi-520\li1560\fc1\cf1 -O Optimize. Optimizing compilation takes somewhat more time, and a lot more memory for a large function.\
\
Without -O, the compiler's goal is to reduce the cost of compilation and to make debugging produce the expected results. With -O, the compiler tries to reduce code size and execution time. Some of the -f options described below turn specific kinds of optimization on or off.\
\pard\tx1540\tx1800\tx2240\tx2800\tx3360\tx3920\tx4480\tx4580\tx5040\tx5600\f1\fs24\fi-520\li1560\fc0\cf0 -g Produce debugging information in GDB format.\
\
Unlike most other C compilers, GNU CC allows you to use -g with -O. The shortcuts taken by optimized code may occasionally produce surprising results: Some variables you declared may not exist at all; flow of control may briefly move where you didn't expect it; some statements may not be executed because they compute constant results or their values were already at hand; some statements may execute in different places because they were moved out of loops. Nevertheless, this makes it possible to debug optimized output if necessary.\
\pard\tx960\tx1920\tx2880\tx3840\tx4800\tx5760\tx6720\tx7680\tx8640\tx9600\fc1\cf1 The fastest way to pursue one missing variable would be to recompile the troublesome source file from the shell, without the -O flag. Then relink, and debug the new executable. \